package Amazon;

import java.util.Arrays;

public class abs_max {
	
	/**
	 * Find the max of F(S) = abs(S[0]-S[1]) + abs(S[1]-S[2]) + abs(S[2]-S[3])
	 * @param A
	 * @param B
	 * @param C
	 * @param D
	 * @return
	 */
	public static int solution(int A, int B, int C, int D){
		int[] arr = new int[4];
		arr[0]=A;arr[1]=B;arr[2]=C;arr[3]=D;
		
		int max = 0;
		for(int i=0;i<4;++i){
			for(int j=0;j<4;++j){
				if(j==i) continue;
				for(int k=0;k<4;++k){
					if(k==j || k==i) continue;
					for(int l=0;l<4;++l){
						if(l==i || l==j || l==k) continue;
						int fs = Math.abs(arr[i]-arr[j]) + Math.abs(arr[j]-arr[k]) + Math.abs(arr[k]-arr[l]);
						if(fs>max) max = fs;
					}
				}
			}
		}
		return max;
	}
	
	public static int solution2(int A, int B, int C, int D){
		int[] arr = new int[4];
		arr[0]=A;arr[1]=B;arr[2]=C;arr[3]=D;
		Arrays.sort(arr);
		int max = Math.abs(arr[1]-arr[3])+Math.abs(arr[3]-arr[0])+Math.abs(arr[0]-arr[2]);
		return max;
	}
	
	public static int solution3(int A, int B, int C, int D){
		int t;
        if(C<A)
			{t=C;C=A;A=t;}
		if(C<B)
			{t=C;C=B;B=t;}
		if(C<D)
			{t=C;C=D;D=t;}
		if(A<B)
			{t=A;A=B;B=t;}
		if(A<D)
			{t=A;A=D;D=t;}
		if(D<B)
			{t=D;D=B;B=t;}
		return A-B+C-B+C-D;
	}
	

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		System.out.println(solution(2,5,8,10));
		System.out.println(solution2(2,5,8,10));
		System.out.println(solution3(2,5,8,10));
	}

}
